<?php

require_once 'phpmoko/utils/UrlGenerator.php';

class Curl
{
	/**
	 * @var UrlGenerator
	 */
	private $url;
	
	/**
	 * @var array
	 */
	private $parametersPost = array();
	
	/**
	 * @param string $url
	 */
	function __construct($url)
	{
		$this->url = new UrlGenerator($url);
	}

	/**
	 * @return string
	 */
	public function getUrl()
	{
		return $this->url->getUrl();
	}

	/**
	 * @param string $url
	 */
	public function setUrl($url)
	{
		$this->url->setUrl($url);
	}

	/**
	 * @param string $name
	 * @param string $value
	 * @param string $method - 'GET' Similar than ?name=value&... and 'POST' Are sended than a Form
	 */
	public function setParameter($name, $value, $method = 'GET')
	{
		if (strtolower($method) == 'post')
			$this->parametersPost[$name] = $value;
		else
			$this->url->setParameter($name, $value);
	}

	/**
	 * @return string
	 */
	public function exec()
	{
		$ch = curl_init($this->url->toString());
		$useragent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5';
		curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $this->parametersPost);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$output = curl_exec($ch);
		curl_close($ch);

		return $output;
	}
}
?>
